package com.junzhiit.nengji.shop.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.junzhiit.nengji.bo.FavoriteShopBO;
import com.junzhiit.nengji.dao.FavoriteShopDAO;
import com.junzhiit.nengji.dao.ShopDAO;
import com.junzhiit.nengji.dao.UserDAO;
import com.junzhiit.nengji.model.FavoriteShop;
import com.junzhiit.nengji.model.Shop;
import com.junzhiit.nengji.shop.service.FavoriteShopService;
import com.junzhiit.nengji.shop.web.exception.UserNotFoundException;
import com.junzhiit.nengji.shop.web.util.APICodeStatus;
import com.junzhiit.nengji.shop.web.vo.FavoriteShopVO;
import com.junzhiit.nengji.shop.web.vo.ListVO;
import com.junzhiit.nengji.shop.web.vo.ResultVO;

@Service
public class FavoriteShopServiceImpl extends BaseServiceImpl implements FavoriteShopService {
	
	@Autowired
	private UserDAO userDAO;
	
	@Autowired
	private FavoriteShopDAO favoriteShopDAO;
	
	@Autowired
	private ShopDAO shopDAO;
	
	public ResultVO add(String shopId, String userId) {
		ResultVO result = new ResultVO();
		int userCount = userDAO.checkMember(userId);
		if(userCount == 0)
			throw new UserNotFoundException("User not found.");
		
		int goodsCount = shopDAO.checkShop(shopId);
		if(goodsCount == 0){
			result.setStatus(APICodeStatus.SHOP_NOT_FOUND);
			result.setMessage("shop not found.");
			return result;
		}
		
		FavoriteShop favoriteShop = favoriteShopDAO.getFavoriteShop(userId, shopId);
		if(favoriteShop!=null && StringUtils.isNotBlank(favoriteShop.getId())){
			result.setData(favoriteShop.getId());
		}else{
			favoriteShop = new FavoriteShop();
			favoriteShop.setShopId(shopId);
			favoriteShop.setUserId(userId);
			int max = shopDAO.checkMaxCollections(shopId);
			Shop shop = new Shop();
			shop.setId(shopId);
			shop.setCollections(max+1);
			shopDAO.update(shop);
			favoriteShop = favoriteShopDAO.insert(favoriteShop);
			FavoriteShopBO favoriteShopBO = new FavoriteShopBO();
			favoriteShopBO.setFavoriteShopId(favoriteShop.getId());
			favoriteShopBO.setCollections(max+1);
			result.setData(favoriteShopBO);
		}
		return result;
	}

	@Transactional
	public ResultVO delete(String userId, String favoriteShopId) {
		ResultVO result = new ResultVO();
		int memberCount = userDAO.checkMember(userId);
		if(memberCount == 0)
			throw new UserNotFoundException("User not found.");
		
        String[] sourceStrArray = favoriteShopId.split(",");
        for (int i = 0; i < sourceStrArray.length; i++) {
        	FavoriteShop favoriteShop = favoriteShopDAO.query(sourceStrArray[i]);
        	if(favoriteShop != null){
        		int max = shopDAO.checkMaxCollections(favoriteShop.getShopId());
        		Shop shop = new Shop();
        		shop.setId(favoriteShop.getShopId());
        		shop.setCollections(max-1);
        		shopDAO.update(shop);
        		
        		favoriteShopDAO.updateToDelete(sourceStrArray[i]);
        	}
        	
        	
        }
		return result;
	}

	public ResultVO getFavoriteShop(Integer pageNumber,Integer maxPageRow,String userId,String shopId) {
		ResultVO result = new ResultVO();
		int memberCount = userDAO.checkMember(userId);
		if(memberCount == 0)
			throw new UserNotFoundException("User not found.");
		
		List<FavoriteShopBO> list = favoriteShopDAO.getFavoriteShopList(pageNumber, maxPageRow, userId,shopId);
		if(list.size() == 0){
			result.setStatus(APICodeStatus.FAVORITE_SHOP_NOT_FOUND);
			result.setMessage("favorite shop not found.");
			return result;
		}
		
		int totalCount = favoriteShopDAO.getFavoriteShopTotalCount(userId);
 		List<FavoriteShopVO> favoriteListVO = new ArrayList<FavoriteShopVO>();
 		
 		for(FavoriteShopBO favoriteShopBO : list){
 			FavoriteShopVO params = new FavoriteShopVO();
 			BeanUtils.copyProperties(favoriteShopBO,params);
 			favoriteListVO.add(params);
		}
 		ListVO listVO = new ListVO();
 		listVO.setIndex(pageNumber);
		listVO.setCount(totalCount);
		listVO.setList(favoriteListVO);
		
 		result.setData(listVO);
		return result;
	}
	
}
